package org.share.comm.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * Sqlite3相关操作工具类
 * 
 * @author hhm
 *
 */
public class SqliteDBUtils extends DBUtils {
	
	/**
	 * 根据sqlite数据库位置打开数据库，获取连接
	 * 
	 * @return
	 */
	public static Connection getConnection(String dbpath) {
		Connection conn = null;
		String url = "jdbc:sqlite:" + dbpath;

		try {
			Class.forName("org.sqlite.JDBC");
			conn = DriverManager.getConnection(url);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static boolean tableExists(String dbpath, String tablename) throws SQLException {
		boolean flag = false;
		String sql = String.format("select count(*) c from sqlite_master where type='table' and name='%s'", tablename);
		Object obj = executeQueryOne(getConnection(dbpath), sql)[0];
		int i = Integer.parseInt(String.valueOf(obj));
		if (i == 1) {
			flag = true;
		}

		return flag;
	}
}
